package com.u3k.mid;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class ak {
    private static final char[] a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
    private static final int[] b = new int[256];

    static {
        Arrays.fill(b, -1);
        int length = a.length;
        for (int i = 0; i < length; i++) {
            b[a[i]] = i;
        }
        b[61] = 0;
    }

    public static String a(String str, byte[] bArr) {
        return b(a(str.getBytes(), bArr));
    }

    private static void a(byte[] bArr, InputStream inputStream, OutputStream outputStream, int i) {
        Key secretKeySpec = new SecretKeySpec(bArr, "AES");
        Cipher instance = Cipher.getInstance("AES/ECB/PKCS5Padding");
        instance.init(i, secretKeySpec);
        byte[] bArr2 = new byte[1024];
        while (true) {
            int read = inputStream.read(bArr2, 0, 1024);
            if (read != -1) {
                outputStream.write(instance.update(bArr2, 0, read));
            } else {
                outputStream.write(instance.doFinal());
                return;
            }
        }
    }

    public static byte[] a(String str, String str2) {
        return b(str.getBytes(), c(str2.getBytes("UTF-8")));
    }

    private static byte[] a(byte[] bArr) {
        return bArr == null ? new byte[0] : a(bArr, false);
    }

    public static final byte[] a(byte[] bArr, boolean z) {
        int i = 0;
        int length = bArr != null ? bArr.length : 0;
        if (length == 0) {
            return new byte[0];
        }
        int i2 = (length / 3) * 3;
        int i3 = (((length - 1) / 3) + 1) << 2;
        int i4 = i3 + (z ? ((i3 - 1) / 76) << 1 : 0);
        byte[] bArr2 = new byte[i4];
        int i5 = 0;
        int i6 = 0;
        int i7 = 0;
        while (i5 < i2) {
            int i8 = i5 + 1;
            byte b = bArr[i5];
            int i9 = i8 + 1;
            i5 = i9 + 1;
            i8 = (((bArr[i8] & 255) << 8) | ((b & 255) << 16)) | (bArr[i9] & 255);
            int i10 = i6 + 1;
            bArr2[i6] = (byte) a[(i8 >>> 18) & 63];
            i6 = i10 + 1;
            bArr2[i10] = (byte) a[(i8 >>> 12) & 63];
            i10 = i6 + 1;
            bArr2[i6] = (byte) a[(i8 >>> 6) & 63];
            i6 = i10 + 1;
            bArr2[i10] = (byte) a[i8 & 63];
            if (z) {
                i7++;
                if (i7 == 19 && i6 < i4 - 2) {
                    i7 = i6 + 1;
                    bArr2[i6] = (byte) 13;
                    bArr2[i7] = (byte) 10;
                    i6 = i7 + 1;
                    i7 = 0;
                }
            }
        }
        i5 = length - i2;
        if (i5 > 0) {
            byte b2 = bArr[i2];
            if (i5 == 2) {
                i = (bArr[length - 1] & 255) << 2;
            }
            i |= (b2 & 255) << 10;
            bArr2[i4 - 4] = (byte) a[i >> 12];
            bArr2[i4 - 3] = (byte) a[(i >>> 6) & 63];
            bArr2[i4 - 2] = i5 == 2 ? (byte) a[i & 63] : (byte) 61;
            bArr2[i4 - 1] = (byte) 61;
        }
        return bArr2;
    }

    public static byte[] a(byte[] bArr, byte[] bArr2) {
        return a(bArr, bArr2, 1);
    }

    private static byte[] a(byte[] bArr, byte[] bArr2, int i) {
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream(bArr2.length);
        InputStream byteArrayInputStream = new ByteArrayInputStream(bArr2);
        try {
            a(bArr, byteArrayInputStream, byteArrayOutputStream, i);
            byte[] toByteArray = byteArrayOutputStream.toByteArray();
            return toByteArray;
        } finally {
            byteArrayInputStream.close();
            byteArrayOutputStream.flush();
            byteArrayOutputStream.close();
        }
    }

    private static String b(byte[] bArr) {
        if (bArr == null) {
            return "";
        }
        try {
            return new String(a(bArr), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static byte[] b(byte[] bArr, byte[] bArr2) {
        return a(bArr, bArr2, 2);
    }

    private static byte[] c(byte[] bArr) {
        if (bArr == null) {
            return new byte[0];
        }
        byte[] d = d(bArr);
        return d == null ? new byte[0] : d;
    }

    private static final byte[] d(byte[] bArr) {
        int i = 0;
        int length = bArr.length;
        if (length == 0) {
            return new byte[0];
        }
        int i2 = length - 1;
        int i3 = 0;
        while (i3 < i2 && b[bArr[i3] & 255] < 0) {
            i3++;
        }
        int i4 = i2;
        while (i4 > 0 && b[bArr[i4] & 255] < 0) {
            i4--;
        }
        int i5 = bArr[i4] == (byte) 61 ? bArr[i4 + -1] == (byte) 61 ? 2 : 1 : 0;
        int i6 = (i4 - i3) + 1;
        if (length > 76) {
            i2 = (bArr[76] == (byte) 13 ? i6 / 78 : 0) << 1;
        } else {
            i2 = 0;
        }
        int i7 = (((i6 - i2) * 6) >> 3) - i5;
        byte[] bArr2 = new byte[i7];
        int i8 = i7 / 3;
        i6 = 0;
        int i9 = i3;
        i3 = 0;
        while (i6 < i8 * 3) {
            int i10 = i9 + 1;
            int i11 = i10 + 1;
            int i12 = i11 + 1;
            int i13 = i12 + 1;
            i10 = b[bArr[i12]] | (((b[bArr[i9]] << 18) | (b[bArr[i10]] << 12)) | (b[bArr[i11]] << 6));
            i9 = i6 + 1;
            bArr2[i6] = (byte) (i10 >> 16);
            i6 = i9 + 1;
            bArr2[i9] = (byte) (i10 >> 8);
            i9 = i6 + 1;
            bArr2[i6] = (byte) i10;
            if (i2 > 0) {
                i6 = i3 + 1;
                if (i6 == 19) {
                    i6 = i9;
                    i9 = i13 + 2;
                    i3 = 0;
                } else {
                    i3 = i6;
                    i6 = i9;
                    i9 = i13;
                }
            } else {
                i6 = i9;
                i9 = i13;
            }
        }
        if (i6 < i7) {
            int i14 = i9;
            i9 = 0;
            i2 = i14;
            while (i2 <= i4 - i5) {
                i9 |= b[bArr[i2]] << (18 - (i * 6));
                i2++;
                i++;
            }
            i2 = 16;
            while (i6 < i7) {
                bArr2[i6] = (byte) (i9 >> i2);
                i2 -= 8;
                i6++;
            }
        }
        return bArr2;
    }
}
